package cn.ycxy.lbl.posts.config;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

/**
 * @Author BaoL
 * @Date 2023/3/15 9:37
 * @Description Spring MVC配置类
 * @Since version-1.0
 */
@Slf4j
@Configuration
public class WebMvcConfiguration implements WebMvcConfigurer {

    @Value("${image.window.filePath}")
    private String windowFilePath;
    @Value("${image.linux.filePath}")
    private String linuxFilePath;
    public WebMvcConfiguration() {
        log.debug("创建配置类对象：WebMvcConfiguration");
    }


    /**
     * 将物理磁盘文件存放的绝对路径映射为一个虚拟路径，就可以通过SpringBoot服务来访问文件了
     * @param registry
     */
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        String os = System.getProperty("os.name");
        if (os.toLowerCase().startsWith("win")) {  //如果是Windows系统
            log.info("Windows系统启动...");
            // 例:资源访问路径为:http://localhost:9001/images/img_add.png
            registry.addResourceHandler("/profile/**").  //设置项目访问的虚拟映射路径虚拟路径/images
                    //将D:/learming/git-ruoyi/uploadPath/路径下的所有资源文件全部映射到 /images/下
                            addResourceLocations("file:" + windowFilePath);
        }else{//linux和mac系统
            log.info("Linux或Mac系统启动...");
            registry.addResourceHandler("/profile/**").
                    addResourceLocations("file:" + linuxFilePath);
        }
    }

    @Override
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/**")
                .allowedOriginPatterns("*")
                .allowedHeaders("*")
                .allowedMethods("*")
                .allowCredentials(true)
                .maxAge(3600);
    }

}